﻿using System;
using System.Configuration;
using HelpersDotNet.DomainDrivenDesign.Infrastructure.RepositoryFramework.Configuration;
using HelpersDotNet.DomainDrivenDesign.Infrastructure.Transactions;

namespace HelpersDotNet.DomainDrivenDesign.Infrastructure.RepositoryFramework
{
    public static class ClientTransactionRepositoryFactory
    {
        private static IClientTransactionRepository _transactionRepository;

        public static IClientTransactionRepository GetTransactionRepository()
        {
            if (_transactionRepository == null)
            {
                RepositorySettings settings =
                    (RepositorySettings)ConfigurationManager.GetSection(
                    RepositoryMappingConstants.RepositoryMappingsConfigurationSectionName);

                Type repositoryType =
                    Type.GetType(
                    settings.RepositoryMappings[RepositoryMappingConstants.IClientTransactionAttributeName].RepositoryFullTypeName);

                _transactionRepository = Activator.CreateInstance(repositoryType) as IClientTransactionRepository;
            }
            return _transactionRepository;
        }
    }
}
